import Taro from "@tarojs/taro";
export var openConfirm = function () {
    return new Promise(function (resolve) {
        Taro.showModal({
            content: "您没有打开麦克风和摄像头的权限，是否去设置打开？",
            confirmText: "确认",
            cancelText: "取消",
            success: function (res) {
                if (res.confirm) {
                    console.log("用户点击确认");
                    Taro.openSetting({
                        success: function (res) {
                            console.log("手动开启摄像头录音功能：", res.authSetting);
                            var authorizeMic = res.authSetting["scope.record"];
                            var authorizeCamera = res.authSetting["scope.camera"];
                            if (authorizeMic && authorizeCamera) {
                                resolve(true);
                            }
                            else {
                                resolve(false);
                            }
                        }
                    });
                }
                else if (res.cancel) {
                    console.log("用户点击取消");
                    Taro.showToast({
                        title: "拒绝授权,无法进入视频！",
                        icon: "none",
                        duration: 2000
                    });
                }
            }
        });
    });
};
export var checkDeviceAuthorize = function () {
    return new Promise(function (resolve, reject) {
        if (!Taro.getSetting || !Taro.getSetting()) {
            // 微信测试版 获取授权API异常，目前只能即使没授权也可以通过
            resolve(false);
        }
        Taro.getSetting().then(function (result) {
            var authorizeMic = result.authSetting["scope.record"];
            var authorizeCamera = result.authSetting["scope.camera"];
            if (result.authSetting["scope.camera"] &&
                result.authSetting["scope.record"]) {
                // 授权成功
                resolve(true);
            }
            else {
                // 没有授权，弹出授权窗口
                // 注意： wx.authorize 只有首次调用会弹框，之后调用只返回结果，如果没有授权需要自行弹框提示处理
                console.log("getSetting 没有授权录音/摄像头，弹出授权窗口", result);
                Taro.authorize({
                    scope: "scope.record"
                })
                    .then(function (res) {
                    console.log("授权录音", res);
                    authorizeMic = true;
                    if (authorizeCamera) {
                        resolve(true);
                    }
                })
                    .catch(function (error) {
                    console.log("拒绝录音", error);
                    authorizeMic = false;
                });
                Taro.authorize({
                    scope: "scope.camera"
                })
                    .then(function (res) {
                    console.log("授权摄像头", res);
                    authorizeCamera = true;
                    if (authorizeMic) {
                        resolve(true);
                    }
                    else {
                        openConfirm().then(function (conformRes) {
                            console.log("openConfirm-conformRes::=====", conformRes);
                            if (conformRes) {
                                resolve(true);
                            }
                            else {
                                reject(new Error("authorize fail"));
                            }
                        });
                    }
                })
                    .catch(function (error) {
                    console.log("拒绝开启摄像头", error);
                    authorizeCamera = false;
                    openConfirm().then(function (conformRes) {
                        if (conformRes) {
                            resolve(true);
                        }
                        else {
                            reject(new Error("authorize fail"));
                        }
                    });
                });
            }
        });
    });
};
export var checkDeviceVoice = function () {
    return new Promise(function (resolve) {
        Taro.getSetting().then(function (result) {
            if (result.authSetting["scope.record"]) {
                resolve(true);
            }
            else {
                Taro.authorize({
                    scope: "scope.record"
                })
                    .then(function (res) {
                    console.log("授权录音", res);
                    resolve(true);
                })
                    .catch(function (error) {
                    console.log("拒绝开启录音", error);
                    Taro.showModal({
                        content: "您没有打开麦克风的权限，是否去设置打开？",
                        confirmText: "确认",
                        cancelText: "取消",
                        success: function (res) {
                            if (res.confirm) {
                                Taro.openSetting({
                                    success: function (res) {
                                        console.log("手动开启录音功能：", res.authSetting);
                                        var authorizeMic = res.authSetting["scope.record"];
                                        if (authorizeMic) {
                                            resolve(true);
                                        }
                                        else {
                                            resolve(false);
                                        }
                                    }
                                });
                            }
                            else if (res.cancel) {
                                Taro.showToast({
                                    title: "拒绝授权,无法开始录音",
                                    icon: "none",
                                    duration: 2000
                                });
                            }
                        }
                    });
                });
            }
        });
    });
};
//定位检查
export var checkLocationPermission = function () {
    return new Promise(function (resolve) {
        Taro.getSetting().then(function (result) {
            var locationPer = result.authSetting["scope.userLocation"];
            if (locationPer) {
                resolve(true);
            }
            else {
                Taro.authorize({
                    scope: "scope.userLocation"
                })
                    .then(function () {
                    resolve(true);
                })
                    .catch(function () {
                    resolve(false);
                });
            }
        });
    });
};
